﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ZDT.Common
{
    public class AllConverter
    {
        public static T ToEnum<T>(string value)
        {
            return (T)Enum.Parse(typeof(T), value, true);
        }
        public static T ToDefined<T>(object obj)
        {
            T result = default(T);
            if (typeof(T).GetMethod("Parse") != null)
            {
                result = (T)typeof(T).GetMethod("Parse").Invoke(null, new object[] { obj });
                return result;
            }
            if (typeof(T).IsValueType)
            {
                result = (T)obj;
            }
            else if (typeof(T).IsEnum)
            {
                result = ToEnum<T>(obj.ToString());
            }
            else
            {
                result = (T)obj;
            }
            return result;
        }
        public static string Dictionary2Url(IDictionary<string, string> data)
        {
            string result = string.Empty;
            foreach (var item in data)
            {
                if (result.Length > 0)
                {
                    result += "&";
                }
                result += string.Format("{0}={1}", item.Key, item.Value);
            }
            return result;
        }
    }
}
